﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigCreate
{
    /// <summary>
    /// 
    /// </summary>
    public static class ConfigLoader
    {
        /// <summary>
        /// 从配置中还原配置
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="config_file">配置文件</param>
        public static void LoadCreateConfig(this object obj, string? config_file = null)
        {
            if (!string.IsNullOrEmpty(config_file))
            {
                if (!System.IO.Path.IsPathRooted(config_file))
                    config_file = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", config_file);
            }
            else if (obj.GetType().GetCustomAttributes(typeof(CreateConfigPathAttribute), true).FirstOrDefault() is CreateConfigPathAttribute config_path)
            {
                config_file = config_path.ConfigPath;
                if (!System.IO.Path.IsPathRooted(config_file))
                    config_file = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", config_file);
            }
            if (!string.IsNullOrEmpty(config_file))
            {
                ConverterInternal.RestoreBindingObject(obj, config_file);
            }
        }
    }
}
